package org.soarsky.wealthchengdu.module.info;

import org.soarsky.wealthchengdu.R;
import org.soarsky.wealthchengdu.appliction.MyAppliction;
import org.soarsky.wealthchengdu.language.LanguageUtil;
import org.soarsky.wealthchengdu.util.Config;
import org.soarsky.wealthchengdu.view.ALLSendDialog;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class InfomationActivity extends Activity {

	private LinearLayout linearlayout;
	private Bundle bundle;
	// private static String[] dataObjects = new String[] { "聚焦成都", "世说成都",
	// "成都愿景", "成都坐标", "成都概览", "成都镜像", "成都指南" };
	private Horizontal_adapter adapter;
	private LanguageUtil util;
	private String[] list;
	private ImageView inifTitleReturn, inifTitleRefresh;// title 标题返回按钮和刷新按钮

	private int selectID;// 现在的第几个，为刷新准备

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_cd);
		util = Config.changeLanguage(MyAppliction.mCn);
		list = util.getHomeGridNames();

		linearlayout = (LinearLayout) findViewById(R.id.cd_below_linearlayout);
		bundle = this.getIntent().getExtras();
		getaddview(bundle.getInt("data"));
		selectID = bundle.getInt("data");
		MyAppliction.mChoose = bundle.getInt("data");
		HorizontalListView listview = (HorizontalListView) findViewById(R.id.horiaontal_listviwe);
		adapter = new Horizontal_adapter();
		adapter.setlistdata(list);
		listview.setAdapter(adapter);
		if (bundle.getInt("data") + 1 > 4) {
			listview.scrollTo(170 * 4);
		} else {
			listview.scrollTo(170 * bundle.getInt("data"));
		}
		inifTitleReturn = (ImageView) findViewById(R.id.title_ib_c);
		inifTitleRefresh = (ImageView) findViewById(R.id.title_iv_set);
		inifTitleReturn.setImageResource(R.drawable.title_two_lift);
		inifTitleRefresh.setImageResource(R.drawable.title_refresh);
		// listview.setSelection(bundle.getInt("data"));
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				// Toast.makeText(InfomationActivity.this, "sssss" + arg2, 300)
				// .show();

				MyAppliction.mChoose = arg2;
				adapter.notifyDataSetChanged();
				selectID = arg2;
				getaddview(arg2);

			}
		});
		inifTitleRefresh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				getaddview(selectID);
			}
		});
		inifTitleReturn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (FocusCDDataShow.gtextnumber.size() > 0) {
					FocusCDDataShow.gtextnumber.clear();
				}

				InfomationActivity.this.finish();
			}
		});

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		this.finish();
	}

	/**
	 * 获取布局文件
	 * 
	 * @param arg2
	 */
	public void getaddview(int arg2) {

		if (linearlayout.getChildCount() > 0) {
			linearlayout.removeAllViews();
			FocusCDDataShow.gtextnumber.clear();
		}
		if (arg2 == 0 || arg2 == 1 || arg2 == 2 || arg2 == 3 || arg2 == 4) {
			ALLSendDialog.showdialog(this);
			if (arg2 == 1) {
				MyAppliction.mcateID = String.valueOf(104);
			} else if (arg2 == 4) {
				MyAppliction.mcateID = String.valueOf(101);
			} else {
				MyAppliction.mcateID = String.valueOf(arg2 + 100);
			}
			linearlayout.addView(AddViewManager
					.getViewForNormal(InfomationActivity.this));
		} else if (arg2 == 5) {
			ALLSendDialog.showdialog(this);
			MyAppliction.mcateID = String.valueOf(arg2 + 1);
			linearlayout.addView(AddViewManager
					.getViewForImage(InfomationActivity.this));
		} else {
			MyAppliction.mcateID = String.valueOf(7);
			linearlayout.addView(AddViewManager
					.getViewForGuide(InfomationActivity.this));
		}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			if (FocusCDDataShow.gtextnumber != null && FocusCDDataShow.gtextnumber.size() > 0) {
				FocusCDDataShow.gtextnumber.clear();
				
			}
			InfomationActivity.this.finish();
			return true;
		} else
			return super.onKeyDown(keyCode, event);
	}
	

	
}
